package com.neusoft.demo.controller;

import com.neusoft.demo.entity.User;
import com.neusoft.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouQF
 * @description abc
 * @date 2019/9/11
 */
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private Queue queue;

    @Autowired
    private Queue queue3;

    @Autowired
    private Topic topic;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping(value= "/demo")
    public String demo(User user, HttpServletRequest httpServletRequest){
        System.out.println("ip:" + httpServletRequest.getRemoteAddr());
        httpServletRequest.getHeader("X-Real-IP");
        //jmsMessagingTemplate.convertAndSend(queue, "abc");
        //return userService.demo(user);
        return "haha";
    }

    @RequestMapping("/demo1")
    public Map<String, Object> demo1(User user){
        //jmsMessagingTemplate.convertAndSend(queue, "abc");
        Map<String, Object> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        return map;
    }

    @RequestMapping("/sendQueue")
    public List<User> sendQueue(@RequestParam String msg){
        jmsMessagingTemplate.convertAndSend(queue, msg);
        return null;
    }

    @RequestMapping("/sendQueue3")
    public List<User> sendQueue1(@RequestParam String msg){
        jmsMessagingTemplate.convertAndSend(queue3, msg);
        return null;
    }

    @RequestMapping("/sendTopic")
    public List sendTopic(@RequestParam String msg){
        jmsMessagingTemplate.convertAndSend(topic,msg);
        return null;
    }

    @RequestMapping("/testQueryIndex")
    public void testQueryIndex() throws IOException {
        userService.queryIndex();
    }










    @RequestMapping("/userList")
    public Map<String, Object> userList(HttpServletRequest request, User user) {
        return userService.userList(request, user);
    }

    @RequestMapping("/saveUser")
    public String saveUser(@RequestBody User user){
        return userService.saveUser(user);
    }
}
